package com.jinsilu.freight.ui.activity;

import android.text.Html;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;

import com.blankj.utilcode.util.ObjectUtils;
import com.hjq.base.BaseDialog;
import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.jinsilu.freight.R;
import com.jinsilu.freight.aop.SingleClick;
import com.jinsilu.freight.common.MyVBActivity;
import com.jinsilu.freight.databinding.ActivityUploadVehicleLicenseBinding;
import com.jinsilu.freight.helper.CommonHelper;
import com.jinsilu.freight.helper.ShowImageHelper;
import com.jinsilu.freight.http.model.HttpData;
import com.jinsilu.freight.http.model.HttpListData;
import com.jinsilu.freight.http.request.GetLicensePlateColorApi;
import com.jinsilu.freight.http.request.GetVehicleLicenseApi;
import com.jinsilu.freight.http.request.GetVehicleLengthApi;
import com.jinsilu.freight.http.request.OCRApi;
import com.jinsilu.freight.http.request.UpdateImageApi;
import com.jinsilu.freight.http.request.VerifyVehicleLicenseApi;
import com.jinsilu.freight.http.response.FileBean;
import com.jinsilu.freight.http.response.IdCardBean;
import com.jinsilu.freight.http.response.LicensePlateColor;
import com.jinsilu.freight.http.response.OCRDrivingBean;
import com.jinsilu.freight.http.response.OCRIDCardBean;
import com.jinsilu.freight.http.response.VehicleLength;
import com.jinsilu.freight.http.response.VehicleLicenseBean;
import com.jinsilu.freight.other.MTLConstants;
import com.jinsilu.freight.ui.dialog.DateDialog;
import com.jinsilu.freight.ui.dialog.MenuDialog;
import com.king.keyboard.KingKeyboard;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * 上传行驶证
 *
 * @author yangyang
 * @time 2020-11-11 17:35
 */
public class UploadVehicleLicenseActivity extends MyVBActivity<ActivityUploadVehicleLicenseBinding> {

    private final String TAG = getClass().getName();
    private int mCurrentSelectIndex; // 当前选择上传的图片条目索引

    private String frontImage;
    private String backImage;
    private String vehicleFrontPhoto; // 车头照片地址

    private KingKeyboard vehicleKeyBoard;

    private List<LicensePlateColor> licensePlateColors; // 车牌颜色列表
    private String licensePlateColor = ""; // 车牌颜色
    private String licensePlateColorCode = ""; // 车牌颜色编码

    private VehicleLicenseBean mVehicleLicenseBean;

    private BaseDialog licensePlateColorDialog;
    private BaseDialog vehicleLengthDialog; // 车辆长度选择对话框

    private List<VehicleLength> vehicleLengths; // 车辆长度列表
    private String selectedVehicleLength = ""; // 选中的车辆长度
    private String selectedVehicleLengthCode = ""; // 选中的车辆长度编码

    @Override
    protected void initView() {
        // binding.tvNotificationTips.setVisibility(View.VISIBLE);
        // binding.tvNotificationTips.setText("拍摄图片时，请保持画面文字的清晰。");

        setOnClickListener(binding.tvExpirationDate, binding.tvLicensePlateNumber, binding.ivVehicleLicense,
                binding.ivVehicleLicenseCopy, binding.tvBtnSubmitCertificate, binding.tvVehicleLength,
                binding.ivVehicleHeadImg, binding.tvVehicleHeadImg);

        vehicleKeyBoard = new KingKeyboard(this, binding.keyboardParent);
        vehicleKeyBoard.register(binding.etVehicleLicenseNumber, KingKeyboard.KeyboardType.LICENSE_PLATE);
    }

    @Override
    protected void initData() {
        getLicensePlateColor();
        getVehicleLicenseStatus();
        getVehicleLength();
        initStep();
    }

    private void initStep() {
        binding.layoutCertification.tvCertificationTips.setText("请使用本人身份信息进行认证，若使用他人身份认证并接单，将造成运费无法提现");
        binding.layoutCertification.tvCertificationTips.setVisibility(View.GONE);
        binding.tvCheckTheInformation.setText(Html.fromHtml(getString(R.string.tips_check_the_information)));
        binding.layoutCertification.stepView.go(MTLConstants.STEP_FOUR, true);
    }

    /**
     * 获取行驶证状态
     */
    private void getVehicleLicenseStatus() {
        EasyHttp.get(this)
                .api(new GetVehicleLicenseApi())
                .request(new HttpCallback<HttpData<VehicleLicenseBean>>(this) {

                    @Override
                    public void onSucceed(HttpData<VehicleLicenseBean> data) {
                        mVehicleLicenseBean = data.getData();
                        setCertificateStatus(mVehicleLicenseBean);
                    }
                });
    }

    private void setCertificateStatus(VehicleLicenseBean vehicleLicenseBean) {
        switch (vehicleLicenseBean.getVehicleLicenseCertificationStatus()) {
            case -2: // 未认证
                break;
            case 0: // 认证失败
                // binding.layoutCertification.ivCertificationResult.setImageResource(R.drawable.approved_rejected_ic);
                // binding.layoutCertification.ivCertificationResult.setVisibility(View.VISIBLE);
                // binding.layoutCertification.tvCertificationStatus.setText("审核未通过");
                // binding.layoutCertification.tvCertificationStatus.setVisibility(View.VISIBLE);
                binding.layoutCertification.tvCertificationTips.setText(
                        TextUtils.isEmpty(vehicleLicenseBean.getOpinion()) ? "" : vehicleLicenseBean.getOpinion());
                binding.layoutCertification.tvCertificationTips.setVisibility(View.VISIBLE);
                setContentData(vehicleLicenseBean);
                break;
            case 1: // 认证成功
                // binding.layoutCertification.ivCertificationResult.setImageResource(R.drawable.approved_ic);
                // binding.layoutCertification.ivCertificationResult.setVisibility(View.VISIBLE);
                // binding.layoutCertification.tvCertificationStatus.setText("审核通过");
                // binding.layoutCertification.tvCertificationStatus.setVisibility(View.VISIBLE);
                disableClicked(getString(R.string.tips_approved));
                setContentData(vehicleLicenseBean);
                break;
            case -1: // 提交全部认证资料 认证中
                // binding.layoutCertification.ivCertificationResult.setImageResource(R.drawable.approving_ic);
                // binding.layoutCertification.ivCertificationResult.setVisibility(View.VISIBLE);
                // binding.layoutCertification.tvCertificationStatus.setText("审核中");
                // binding.layoutCertification.tvCertificationStatus.setVisibility(View.VISIBLE);
                disableClicked(getString(R.string.tips_approving));
                setContentData(vehicleLicenseBean);
                break;
            default:
                break;
        }
    }

    /**
     * 设置不允许点击
     */
    private void disableClicked(String tips) {
        binding.tvLicensePlateNumber.setEnabled(false);
        binding.ivVehicleLicense.setEnabled(false);
        binding.ivVehicleLicenseCopy.setEnabled(false);
        binding.etVehicleLicenseNumber.setEnabled(false);
        binding.etApprovedLoad.setEnabled(false);
        binding.etTotalMass.setEnabled(false);
        binding.tvExpirationDate.setEnabled(false);

        // 禁用新增的3个功能入口
        binding.tvVehicleLength.setEnabled(false);
        binding.etVehicleIdentificationNumber.setEnabled(false);
        binding.ivVehicleHeadImg.setEnabled(false);
        binding.tvVehicleHeadImg.setEnabled(false);

        binding.layoutCertification.stepView.done(true);

        binding.layoutCertification.tvCompleteCanTakeOrder.setText(tips);
        binding.layoutCertification.tvCertificationTips.setVisibility(View.GONE);

        binding.tvVehicleLicenseTips.setVisibility(View.GONE);
        binding.tvVehicleLicenseCopyTips.setVisibility(View.GONE);
    }

    /**
     * 数据回填
     *
     * @param vehicleLicenseBean 身份数据
     */
    private void setContentData(VehicleLicenseBean vehicleLicenseBean) {
        frontImage = vehicleLicenseBean.getDrivingLicenseFrontUrl();
        backImage = vehicleLicenseBean.getDrivingLicenseReverseUrl();
        if (!TextUtils.isEmpty(frontImage))
            ShowImageHelper.showNormal(this, vehicleLicenseBean.getDrivingLicenseFrontUrl(), binding.ivVehicleLicense);
        if (!TextUtils.isEmpty(backImage))
            ShowImageHelper.showNormal(this, vehicleLicenseBean.getDrivingLicenseReverseUrl(),
                    binding.ivVehicleLicenseCopy);
        if (!TextUtils.isEmpty(vehicleLicenseBean.getLicensePlateColor()))
            licensePlateColor = vehicleLicenseBean.getLicensePlateColor();
        if (!TextUtils.isEmpty(vehicleLicenseBean.getLicensePlateColorCode()))
            licensePlateColorCode = vehicleLicenseBean.getLicensePlateColorCode();
        binding.tvLicensePlateNumber.setText(vehicleLicenseBean.getLicensePlateColor());
        binding.etVehicleLicenseNumber.setText(vehicleLicenseBean.getPlateNumber());
        binding.etVehicleIdentificationCode.setText(vehicleLicenseBean.getDrivingLicenseIdCode());

        binding.etApprovedLoad.setText(vehicleLicenseBean.getApprovedLoad());
        binding.etTotalMass.setText(vehicleLicenseBean.getGrossMass());
        binding.tvExpirationDate.setText(vehicleLicenseBean.getValidityEndDate());
        binding.tvBtnSubmitCertificate.setEnabled(true);

        // 新增字段数据回填
        if (!TextUtils.isEmpty(vehicleLicenseBean.getVehicleLength())) {
            selectedVehicleLength = vehicleLicenseBean.getVehicleLength();
            binding.tvVehicleLength.setText(selectedVehicleLength);
        }
        if (!TextUtils.isEmpty(vehicleLicenseBean.getVehicleFrameNumber())) {
            binding.etVehicleIdentificationNumber.setText(vehicleLicenseBean.getVehicleFrameNumber());
        }
        if (!TextUtils.isEmpty(vehicleLicenseBean.getVehicleFrontPhoto())) {
            vehicleFrontPhoto = vehicleLicenseBean.getVehicleFrontPhoto();
            ShowImageHelper.showNormal(this, vehicleLicenseBean.getVehicleFrontPhoto(), binding.ivVehicleHeadImg);
        }
    }

    @SingleClick
    @Override
    public void onClick(View v) {
        if (v == binding.tvExpirationDate) {
            showDateDialog();
        } else if (binding.tvLicensePlateNumber.equals(v)) {
            if (null != licensePlateColorDialog)
                licensePlateColorDialog.show();
            else
                getLicensePlateColor();
        } else if (v == binding.ivVehicleLicense) {
            mCurrentSelectIndex = 0;
            showTipsDialog();
        } else if (v == binding.ivVehicleLicenseCopy) {
            mCurrentSelectIndex = 1;
            showTipsDialog();
        } else if (v == binding.ivVehicleHeadImg || v == binding.tvVehicleHeadImg) {
            mCurrentSelectIndex = 2;
            startImageSelectActivity(0);
        } else if (v == binding.tvVehicleLength) {
            showVehicleLengthDialog();
        } else if (v == binding.tvBtnSubmitCertificate) {
            if (null == mVehicleLicenseBean) {
                toast(R.string.error_certification_failed_with_network);
                return;
            }
            switch (mVehicleLicenseBean.getVehicleLicenseCertificationStatus()) {
                // -2---未认证，-1---待审核，0---失败，1---成功
                case -2: // 未认证
                case 0: // 认证失败
                    submitInfo();
                    break;
                case -1: // -待审核
                case 1: // 认证成功
                    toNextSteps();
                    break;
                default:
                    break;
            }
        }
    }

    private void toNextSteps() {
        startActivity(UploadRoadTransportActivity.class);
    }

    private BaseDialog tipsDialog;

    private void showTipsDialog() {
        // 自定义对话框
        if (null == tipsDialog) {
            tipsDialog = new BaseDialog.Builder(this)
                    .setContentView(R.layout.dialog_certificate_tips)
                    .setAnimStyle(BaseDialog.ANIM_SCALE)
                    // .setText(id, "我是预设置的文本")
                    .setGravity(Gravity.BOTTOM)
                    .setOnClickListener(R.id.tv_know, (BaseDialog.OnClickListener<TextView>) (dialog, view) -> {
                        // showMenuDialog();
                        startImageSelectActivity(-1);
                        dialog.dismiss();
                    }

                    )
                    .show();
        } else {
            tipsDialog.show();
        }
    }

    private BaseDialog expirationDialog;

    private void showDateDialog() {
        // 日期选择对话框
        if (null == expirationDialog) {
            expirationDialog = new DateDialog.Builder(this)
                    .setTitle(getString(R.string.date_title))
                    // 确定按钮文本
                    .setConfirm(getString(R.string.common_confirm))
                    // 设置 null 表示不显示取消按钮
                    .setCancel(getString(R.string.common_cancel))
                    // 设置日期
                    // .setDate("2018-12-31")
                    // .setDate("20181231")
                    // .setDate(1546263036137)
                    // 设置年份
                    // .setYear(2018)
                    // 设置月份
                    // .setMonth(2)
                    // 设置天数
                    // .setDay(20)
                    // 不选择天数
                    // .setIgnoreDay()
                    .setListener(new DateDialog.OnListener() {
                        @Override
                        public void onSelected(BaseDialog dialog, int year, int month, int day) {
                            // toast(year + getString(R.string.common_year) + month +
                            // getString(R.string.common_month) + day + getString(R.string.common_day));
                            // 如果不指定时分秒则默认为现在的时间
                            // Calendar calendar = Calendar.getInstance();
                            // calendar.set(Calendar.YEAR, year);
                            // 月份从零开始，所以需要减 1
                            // calendar.set(Calendar.MONTH, month - 1);
                            // calendar.set(Calendar.DAY_OF_MONTH, day);
                            // toast("时间戳：" + calendar.getTimeInMillis());
                            // toast(new SimpleDateFormat("yyyy年MM月dd日
                            // kk:mm:ss").format(calendar.getTime()));
                            binding.tvExpirationDate.setText(String.format("%s-%s-%s", year, month, day));
                        }

                        @Override
                        public void onCancel(BaseDialog dialog) {

                        }
                    })
                    .show();
        } else {
            expirationDialog.show();
        }
    }

    /**
     * 行驶证认证
     */
    private void submitInfo() {
        if (TextUtils.isEmpty(licensePlateColor) || TextUtils.isEmpty(licensePlateColorCode)) {
            toast("请选择车牌颜色");
            return;
        }

        if (TextUtils.isEmpty(frontImage)) {
            toast("请上传驶证正本");
            return;
        }
        if (TextUtils.isEmpty(backImage)) {
            toast("请上传驶证副本");
            return;
        }
        String plateNumber = binding.etVehicleLicenseNumber.getText().toString().trim();
        if (TextUtils.isEmpty(plateNumber)) {
            toast("请输入车牌号");
            return;
        }

        // String idNumber =
        // binding.etVehicleIdentificationCode.getText().toString().trim();
        // if (TextUtils.isEmpty(idNumber)) {
        // toast("请输入车辆识别代码");
        // return;
        // }
        String validate = binding.tvExpirationDate.getText().toString().trim();
        if (TextUtils.isEmpty(validate)) {
            toast("请选择有效期止");
            return;
        }

        String approved = binding.etApprovedLoad.getText().toString().trim();
        if (!TextUtils.isEmpty(approved)) {
            if (Integer.parseInt(approved) < 1000) {
                toast("请输入合法的核定载重");
                return;
            }
        }
        String grossMass = binding.etTotalMass.getText().toString().trim();
        if (!TextUtils.isEmpty(grossMass)) {
            if (Integer.parseInt(grossMass) < 1000) {
                toast("请输入合法的总质量");
                return;
            }
        }

        // 新增字段验证
        if (TextUtils.isEmpty(selectedVehicleLength)) {
            toast("请选择车辆长度");
            return;
        }
        String vehicleFrameNumber = binding.etVehicleIdentificationNumber.getText().toString().trim();
        if (TextUtils.isEmpty(vehicleFrameNumber)) {
            toast("请输入车架号");
            return;
        }
        if (TextUtils.isEmpty(vehicleFrontPhoto)) {
            toast("请上传车头照片");
            return;
        }

        EasyHttp.post(this)
                .api(new VerifyVehicleLicenseApi()
                        .setDrivingLicenseFrontUrl(frontImage)
                        .setDrivingLicenseReverseUrl(backImage)
                        .setPlateNumber(plateNumber)
                        .setLicensePlateColor(licensePlateColor)
                        .setLicensePlateColorCode(licensePlateColorCode)
                        .setValidityEndDate(validate)
                        .setApprovedLoad(approved)
                        .setGrossMass(grossMass)
                        .setDrivingLicenseIdCode(mVehicleLicenseBean.getDrivingLicenseIdCode())
                        .setIssuingAuthority(mVehicleLicenseBean.getIssuingAuthority())
                        .setVehicleOwner(mVehicleLicenseBean.getVehicleOwner())
                        .setEngineNumber(mVehicleLicenseBean.getEngineNumber())
                        .setEnergyType(mVehicleLicenseBean.getEnergyType())
                        .setRegisterDate(mVehicleLicenseBean.getRegisterDate())
                        .setIssueDate(mVehicleLicenseBean.getIssueDate())
                        .setUseCharacter(mVehicleLicenseBean.getUseCharacter())
                        .setVehicleModel(mVehicleLicenseBean.getVehicleModel())
                        .setVehicleType(mVehicleLicenseBean.getVehicleType())
                        .setVehicleLength(selectedVehicleLength)
                        .setVehicleLengthCode(selectedVehicleLengthCode)
                        .setVehicleFrameNumber(vehicleFrameNumber)
                        .setVehicleFrontPhoto(vehicleFrontPhoto))
                .request(new HttpCallback<HttpData<Void>>(this) {

                    @Override
                    public void onSucceed(HttpData<Void> data) {
                        // setResult(RESULT_OK);
                        // finish();
                        toNextSteps();
                    }

                    @Override
                    public void onFail(Exception e) {
                        CommonHelper.toastErrorMsg(e, getString(R.string.plz_reupload_vehicle_image));
                    }
                });
    }

    /**
     * 启动图片选择
     */
    private void startImageSelectActivity(int index) {
        ImageSelectVBActivity.start(this, new ImageSelectVBActivity.OnPhotoSelectListener() {

            @Override
            public void onSelected(File file) {
                // File file = CommonHelper.getFileFromUri(data);
                uploadImage(file);
            }

            @Override
            public void onCancel() {
            }
        });
    }

    private void uploadImage(File file) {
        // 上传文件
        EasyHttp.post(this)
                .api(new UpdateImageApi()
                        .setImage(file))
                .request(new HttpCallback<HttpData<FileBean>>(this) {

                    @Override
                    public void onSucceed(HttpData<FileBean> data) {
                        // FileUtils.delete(file);
                        String imgUrl = data.getData().getUrl();
                        if (mCurrentSelectIndex == 0) {
                            // frontImage = imgUrl;
                            // ShowImageHelper.showNormal(UploadVehicleLicenseActivity.this, imgUrl,
                            // binding.ivVehicleLicense);
                            ocr(MTLConstants.DRIVING_FRONT, imgUrl);
                        } else if (mCurrentSelectIndex == 1) {
                            // backImage = imgUrl;
                            // ShowImageHelper.showNormal(UploadVehicleLicenseActivity.this, imgUrl,
                            // binding.ivVehicleLicenseCopy);
                            ocr(MTLConstants.DRIVING_BACK, imgUrl);
                        } else if (mCurrentSelectIndex == 2) {
                            vehicleFrontPhoto = imgUrl;
                            ShowImageHelper.showNormal(UploadVehicleLicenseActivity.this, imgUrl,
                                    binding.ivVehicleHeadImg);
                        }
                    }
                });
    }

    /**
     * orc 识别
     *
     * @param type   类型【1身份证人像面；2身份证国徽面；3驾驶证正页；4驾驶证副页；5行驶证正页；6行驶证副页；7车辆道路运输证】
     * @param imgUrl 图片地址
     */
    private void ocr(String type, String imgUrl) {
        EasyHttp.post(this)
                .api(new OCRApi()
                        .setType(type)
                        .setUrl(imgUrl))
                .request(new HttpCallback<HttpData<OCRDrivingBean>>(this) {

                    @Override
                    public void onSucceed(HttpData<OCRDrivingBean> data) {
                        OCRDrivingBean ocrDrivingBean = data.getData();
                        if (ObjectUtils.isEmpty(ocrDrivingBean)) {
                            toast(R.string.plz_reupload_vehicle_image);
                            return;
                        }
                        if (mCurrentSelectIndex == 0) {
                            mVehicleLicenseBean.setDrivingLicenseFrontUrl(imgUrl);
                            mVehicleLicenseBean.setDrivingLicenseIdCode(ocrDrivingBean.getVin());

                            mVehicleLicenseBean.setEngineNumber(ocrDrivingBean.getEngineNo());
                            mVehicleLicenseBean.setPlateNumber(ocrDrivingBean.getNumber());
                            mVehicleLicenseBean.setRegisterDate(ocrDrivingBean.getRegisterDate());
                            mVehicleLicenseBean.setUseCharacter(ocrDrivingBean.getUseCharacter());
                            mVehicleLicenseBean.setVehicleModel(ocrDrivingBean.getModel());
                            mVehicleLicenseBean.setVehicleOwner(ocrDrivingBean.getName());
                            mVehicleLicenseBean.setIssuingAuthority(ocrDrivingBean.getIssuingAuthority());
                            mVehicleLicenseBean.setVehicleType(ocrDrivingBean.getVehicleType());
                            mVehicleLicenseBean.setIssueDate(ocrDrivingBean.getIssueDate());
                        } else {
                            mVehicleLicenseBean.setDrivingLicenseReverseUrl(imgUrl);
                            // 替换非数字字符
                            mVehicleLicenseBean.setGrossMass(ocrDrivingBean.getGrossMass());
                            mVehicleLicenseBean.setApprovedLoad(ocrDrivingBean.getApprovedLoad());
                            mVehicleLicenseBean.setEnergyType("柴油");
                            if (!TextUtils.isEmpty(ocrDrivingBean.getInspectionRecord())) {
                                if (ocrDrivingBean.getInspectionRecord().contains("柴油")) {
                                    mVehicleLicenseBean.setEnergyType("柴油");
                                } else if (ocrDrivingBean.getInspectionRecord().contains("天然气")) {
                                    mVehicleLicenseBean.setEnergyType("天然气");
                                }
                            }
                        }
                        setContentData(mVehicleLicenseBean);
                    }

                    @Override
                    public void onFail(Exception e) {
                        toast(R.string.plz_reupload_vehicle_image);
                    }
                });
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (null != vehicleKeyBoard)
            vehicleKeyBoard.onResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (null != vehicleKeyBoard)
            vehicleKeyBoard.onDestroy();
    }

    /**
     * 获取车牌颜色
     */
    private void getLicensePlateColor() {
        EasyHttp.get(this)
                .api(new GetLicensePlateColorApi())
                .request(new HttpCallback<HttpListData<LicensePlateColor>>(this) {

                    @Override
                    public void onSucceed(HttpListData<LicensePlateColor> data) {
                        licensePlateColors = data.getData();
                        List<String> plateColorList = new ArrayList<>();
                        for (LicensePlateColor plateColor : licensePlateColors) {
                            // if ("Y".equals(plateColor.getIsDefault())) // 设置默认车牌颜色
                            // binding.tvLicensePlateNumber.setText(plateColor.getDictLabel());
                            plateColorList.add(plateColor.getDictLabel());
                        }
                        initBottomSelectDialog(plateColorList);
                    }
                });
    }

    private void initBottomSelectDialog(List<String> data) {
        // 底部选择框
        licensePlateColorDialog = new MenuDialog.Builder(this)
                // 设置 null 表示不显示取消按钮
                // .setCancel(getString(R.string.common_cancel))
                // 设置点击按钮后不关闭对话框
                // .setAutoDismiss(false)
                .setList(data)
                .setListener(new MenuDialog.OnListener<String>() {

                    @Override
                    public void onSelected(BaseDialog dialog, int position, String string) {
                        licensePlateColor = licensePlateColors.get(position).getDictLabel();
                        licensePlateColorCode = licensePlateColors.get(position).getDictValue();
                        binding.tvLicensePlateNumber.setText(string);
                    }

                    @Override
                    public void onCancel(BaseDialog dialog) {
                        if (dialog.isShowing())
                            dialog.dismiss();
                    }
                })
                .create();
    }

    /**
     * 获取车辆长度
     */
    private void getVehicleLength() {
        EasyHttp.get(this)
                .api(new GetVehicleLengthApi())
                .request(new HttpCallback<HttpListData<VehicleLength>>(this) {
                    @Override
                    public void onSucceed(HttpListData<VehicleLength> data) {
                        vehicleLengths = data.getData();
                        List<String> lengthList = new ArrayList<>();
                        for (VehicleLength length : vehicleLengths) {
                            lengthList.add(length.getDictLabel());
                        }
                        initVehicleLengthDialog(lengthList);
                    }
                });
    }

    private void initVehicleLengthDialog(List<String> data) {
        vehicleLengthDialog = new MenuDialog.Builder(this)
                .setList(data)
                .setListener(new MenuDialog.OnListener<String>() {
                    @Override
                    public void onSelected(BaseDialog dialog, int position, String string) {
                        selectedVehicleLength = vehicleLengths.get(position).getDictLabel();
                        selectedVehicleLengthCode = vehicleLengths.get(position).getDictValue();
                        binding.tvVehicleLength.setText(string);
                        if (mVehicleLicenseBean != null) {
                            mVehicleLicenseBean.setVehicleLength(string);
                        }
                    }

                    @Override
                    public void onCancel(BaseDialog dialog) {
                        if (dialog.isShowing())
                            dialog.dismiss();
                    }
                })
                .create();
    }

    private void showVehicleLengthDialog() {
        if (null == vehicleLengthDialog) {
            vehicleLengthDialog = new MenuDialog.Builder(this)
                    .setList(vehicleLengths)
                    .setListener(new MenuDialog.OnListener<String>() {
                        @Override
                        public void onSelected(BaseDialog dialog, int position, String string) {
                            selectedVehicleLength = string;
                            binding.tvVehicleLength.setText(string);
                            if (mVehicleLicenseBean != null) {
                                mVehicleLicenseBean.setVehicleLength(string);
                            }
                        }

                        @Override
                        public void onCancel(BaseDialog dialog) {
                            if (dialog.isShowing())
                                dialog.dismiss();
                        }
                    })
                    .create();
        }
        vehicleLengthDialog.show();
    }
}
